package com.great.medical.goods.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.great.medical.classify.db.model.Classify;
import com.great.medical.classify.service.ClassifyService;
import com.great.medical.common.ResultBody;
import com.great.medical.common.StatusEnum;
import com.great.medical.goods.db.model.OriginGoods;
import com.great.medical.goods.db.model.ProtocolGoods;
import com.great.medical.goods.service.OriginGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/originGoods")
@Slf4j
@SuppressWarnings("unchecked")
public class OriginGoodsController {

    @Autowired
    private OriginGoodsService originGoodsService;
    @Autowired
    private ClassifyService classifyService;


    @GetMapping("/getPageList")
    public ResultBody<IPage<OriginGoods>> getPageList(@RequestParam(required = false) Map map) {
        final IPage<OriginGoods> iPage = originGoodsService.getPageList(map);
        for (OriginGoods record : iPage.getRecords()) {
            final Classify classify = classifyService.getById(record.getClassifyId());
            if (!Objects.isNull(classify)) {
                record.setClassifyName(classify.getName());
            }
        }
        return ResultBody.ok().data(iPage);
    }

    @GetMapping("/findByClassifyId")
    public ResultBody<List<OriginGoods>> findByClassifyId(@RequestParam Long classifyId) {
        final List<OriginGoods> list = originGoodsService.lambdaQuery()
                .eq(OriginGoods::getClassifyId, classifyId)
                .eq(OriginGoods::getStatus, StatusEnum.ENABLE.getCode()).list();
        return ResultBody.ok().data(list);
    }


    @PostMapping("/saveOrUpdate")
    public ResultBody<Void> saveOrUpdate(@RequestBody OriginGoods model) {
        originGoodsService.saveOrUpdate(model);
        return ResultBody.ok();
    }


}
